const app = getApp()
var CONFIG = require("../../config/config.js");
var share = require('../../utils/share.js');
Page({
    data: {
        uc_id: 0,
        uc: [],
        pickerpricefalse: false,
        pay_methods: [{ title: "请选择" }], //缴费方式
        pay_method_index: 0, //缴费方式下标
        // pay_types: [{ title: "请选择" }],
        pay_type_title: '',
        moneyfree: [], //类型
        moneyfree_id: 2,
        imgback: '/images/qikeya1.png',
        canvasHidden: false,
        imagePath: [],
        imgUrl: CONFIG.API_PIC,
        showerca: false,
        canvasHidden: false,
        butfalse: true,
        showfalse: false,
        text: '',
        showfalse1: false,
        text1: '',
        // 触摸开始时间
        touchStartTime: 0,
        // 触摸结束时间
        touchEndTime: 0,
        // 最后一次单击事件点击发生时间
        lastTapTime: 0,
        // 单击事件点击后要触发的函数
        lastTapTimeoutFunc: null,
        textsan: '请填写备注',
        ye: '',
        chenorderfalse: false,
        stu_id: '',
        ordertext: '请输入网课订单号',
        orderprice: 0,
        isimage: 1, //是否需要上传图片  1：需要 2不需要
        orderprice: '',
        ordertext: '请输入网课订单号', //网课订单号
        is_img: 3, //1:付款截图  2:网课订单号 3：线上支付
        chenorderfalse: false,
        newconid: '',
        showfalse2:false,
        issuccess:false,
        isfais:false,
        amounts:0
    },

    // 点击取消订单号
    repeatclosefree() {
        var that = this;
        that.setData({
            chenorderfalse: false,
        })
    },

    orderclick() {
        var that = this;
        that.setData({
            chenorderfalse: true,
            ordertext: that.data.ordertext,
        })
    },



    /// 长按
    longTap: function(e) {
        // console.log("long tap")
        var jiaoyanCode = this.data.jiaoyanCode;
        share.sendImage(jiaoyanCode);
    },


    // 选择付款方式
    moneyfreeChange(e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        that.setData({
            moneyfree_id: that.data.moneyfree[index].id,
        })
        if (Number(index) == 1) {
            that.getPayMethods();
        }

    },



    // 获取缴费方式
    getPayMethods() {
        var that = this;
        var formData = {
            // discount_id: that.data.uc.discount_id
             discount_id: ''
        }
        let url=''
        let istype=1
        console.log(that.data.uc.area_id,app.globalData.role.consult_id);
        if(Number(app.globalData.is_dl)==1){
            url='crmapi/User/payMethodDl'
            if(Number(app.globalData.role.consult_id)==(that.data.uc.area_id)){
                // console.log('代理自己校区');
                istype=1
            }else{
                // console.log('代理别人校区');
                istype=2
            }
            formData.type=istype
        }else{
            url='crmapi/user/payMethod'
        }
        share.wxRequest(url, formData, function(res) {
            // console.log(res);
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var obj = [{ title: "请选择" }];
                var methods = obj.concat(data);
                that.setData({
                    pay_methods: methods
                })
            }
        })
    },
    // 点击输入网课订单号
    orderclick() {
        var that = this;
        that.setData({
            chenorderfalse: true
        })
    },
    // 点击确定订单号
    yesbutclick(e) {
        var that = this;
        wx.showLoading({
            title: '正在查看该订单号~',
        })
        var orderid = e.detail.value.orderid;
        var stu = that.data.stu;
        // console.log(orderid);
        if (orderid == '') {
            wx.showToast({
                title: '请输入网课订单号',
                icon: 'none'
            })
        } else {
            var obj = {
                from: 2,
                wkOrderId: orderid,
                user_id: that.data.uc.user_id,
                consultant_id: app.globalData.admin_id,
                user_course_id: that.data.uc_id,
            }
            if (Number(app.globalData.identity) == 3) {
                if (that.data.newconid != '' && Number(that.data.newconid) > 0) {
                    var url = 'crmapi/user/wkOrder'
                } else {
                    var url = 'crmapi/Learn/wkOrder'
                }
            } else {
                var url = 'crmapi/user/wkOrder'
            }
            share.wxRequest(url, obj, function(res) {
                wx.hideLoading();
                if (Number(res.data.errcode) == 0) {
                    var data = res.data.data;
                    that.setData({
                        ordertext: orderid,
                        orderprice: data.res,
                        chenorderfalse: false,
                    })

                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 2500,
                    })
                }
            })
        }
    },
    payMethodChange: function(e) {
        var that = this;
        // console.log(e);
        var index = e.detail.value;
        var pay_methods = that.data.pay_methods;
        that.setData({
            pay_method_index: index,
            is_img: pay_methods[index].is_img
        })
        if (Number(index) == 0) {
            wx.showToast({
                title: '请选择缴费方式',
                icon: 'none'
            })
        }
    },

    // 获取付款方式
    haveoto() {
        var that = this;
        // 获取付款方式
        share.courseotoType(function(res) {
            // console.log(res);
            if (Number(res.data.errcode) == 0) {
                var data1 = res.data.data;
                var obj = [{
                    "id": 0, //付款方式ID
                    "oto_name": "请选择付款方式"
                }]
                var data = obj.concat(data1)
                    // console.log(data)
                that.setData({
                    moneyfree: data
                })
            }
        })
    },
    closeorder(){
        var that = this;
        that.setData({
            showfalse2: false
        })
    },
    openorder(){
        let textarea=this.data.textsan
        let orderprice=this.data.amounts
        this.sharepan(textarea, orderprice)

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        if (options.newconid) {
            var newconid = options.newconid
        } else {
            var newconid = ''
        }
        that.setData({
            uc_id: options.course_id,
            newconid: newconid
                // uc_id: 56,
        });
        that.haveoto();
        //初始化页面数据
        var obj = {
            uc_id: that.data.uc_id
        }
        if (Number(app.globalData.identity) == 3) {
            if (that.data.newconid != '' && Number(that.data.newconid) > 0) {
                var url = 'crmapi/user/userCourse'
            } else {
                var url = 'crmapi/Learn/userCourse'
            }
        } else {
            var url = 'crmapi/user/userCourse'
        }
        share.wxRequest(url, obj, function(res) {
            if (Number(res.data.errcode) == 0) {
                // 缴费类型
                var data = res.data.data;
                that.setData({
                    uc: data,
                    ye: Math.round((data.discounts - data.account_paid) * 1000000) / 1000000
                });
            }
            wx.hideLoading();
        })
    },

    // 用户点击右上角分享
    onShareAppMessage: function() {
        return {
            title: CONFIG.SHARETITLE,
            path: CONFIG.PATHLOGIN
        }
    },

    closeImage() {
        var that = this;
        that.setData({
            showerca: false
        })
        wx.navigateBack();
    },
    //   删除图片
    uploadImage1(e) {
        var that = this;
        var index = e.target.dataset.index;
        var imagePath = that.data.imagePath;
        imagePath.splice(index, 1)
        that.setData({
            imagePath: imagePath
        })
    },

    uploadImage: function() {
        let that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album'],
            success: function(res) {
                var photoSrc = res.tempFilePaths[0];
                share.uploadpic(photoSrc, function(res) {
                    // console.log(res);

                    var path = res.data.path;
                    var imagePath = that.data.imagePath;
                    imagePath.push(path);
                    that.setData({
                        imagePath: imagePath
                    })
                }, function(res) {
                    wx.showToast({
                        title: '上传失败，请重新上传',
                        icon: 'none'
                    })
                })
            }
        })
    },

    bindFormSubmit: function(e) {
        var that = this;
        var price = that.data.ye;
        var textarea = e.detail.value.textarea;

        if (Number(that.data.moneyfree_id) == 0) {
            wx.showToast({
                title: '请选择付款方式',
                icon: 'none'
            })
        } else {
            // 线上
            var moneyfree_id = that.data.moneyfree_id;
            var amount = e.detail.value.amount;
            if (Number(moneyfree_id) == 2) {
                if (amount == '') {
                    wx.showToast({
                        title: '请输入本次课程付款金额',
                        icon: 'none'
                    })
                } else {
                    that.sharepan(textarea, amount)
                }
            } else { //线下
                if (Number(that.data.pay_method_index) == 0) {
                    wx.showToast({
                        title: '请选择缴费方式',
                        icon: 'none'
                    })
                } else {
                    var pay_method = that.data.pay_methods[that.data.pay_method_index].id;
                    if (Number(that.data.is_img) == 2) {
                        if (that.data.ordertext == '请输入网课订单号') {
                            wx.showToast({
                                title: '请输入网课订单号',
                                icon: 'none'
                            })
                        } else {
                            that.insertUserCourse(textarea, '')
                        }
                    }else if (Number(that.data.is_img) == 3&&pay_method==19) {
                        if (amount == '') {
                            wx.showToast({
                                title: '请输入本次课程付款金额',
                                icon: 'none'
                            })
                        }else{
                            that.setData({
                                showfalse2:true,
                                amounts:amount
                            })
                        }

                    } else {
                        if (that.data.imagePath == '') {
                            wx.showToast({
                                title: '请上传付款截图',
                                icon: 'none'
                            })
                        } else if (amount == '') {
                            wx.showToast({
                                title: '请输入本次课程付款金额',
                                icon: 'none'
                            })
                        } else {
                            that.sharepan(textarea, amount)
                        }
                    }
                }
            }
        }


    },
    sharepan(textarea, amount) {
        var that = this;
        var price = that.data.ye;
        var reg = /(^[-+]?[1-9]\d*(\.\d{1,2})?$)|(^[-+]?[0]{1}(\.\d{1,2})?$)/;
        if (amount == '') {
            wx.showToast({
                title: '请输入本次课程付款金额',
                icon: 'none'
            })
        } else if (Number(amount) == 0) {
            wx.showToast({
                title: '本次课程付款金额不能为0',
                icon: 'none'
            })
        } else if (!reg.test(amount)) {
            wx.showToast({
                title: '请输入正确的课程付款金额',
                icon: 'none'
            })
        } else {
            that.insertUserCourse(textarea, amount)
        }
        // else if(price <Number(amount)) {
        //     wx.showToast({
        //         title: '课程付款金额不能大于上次余款',
        //         icon: 'none'
        //     })
        // }
    },


    sureclick() {
        var that = this;
        that.setData({
            butfalse: true,
            showfalse: false,
        })
    },
    sureclick1() {
        var that = this;
        wx.navigateBack();
    },


    insertUserCourse(textarea, amount) {
        var that = this;
        wx.showLoading({
            title: '提交中~',
            mask: true
        })
        that.setData({
            butfalse: false,
        })
        var moneyfree_id = that.data.moneyfree_id;
        if (Number(moneyfree_id) == 2) { //线上
            var pay_method = '';
            var str = '';
            if (amount == '') {
                var amount1 = 0
            } else {
                var amount1 = Number(amount);
            }
        } else {
            var pay_method = that.data.pay_methods[that.data.pay_method_index].id;
            if (Number(that.data.is_img) == 2) {
                var str = '';
                var amount1 = that.data.orderprice;
            } else {
                var imagePath = that.data.imagePath
                if (Number(imagePath.length) > 0) {
                    var str = imagePath.join(',');
                } else {
                    var str = '';
                }
                if (amount == '') {
                    var amount1 = 0
                } else {
                    var amount1 = Number(amount);
                }
            }
        }

        var formData = {
            remark: textarea,
            user_course_id: parseInt(that.data.uc_id),
            pay_method: pay_method,
            amount: amount1,
            jietu: str,
            oto_type: moneyfree_id, //1线下   2线上 
            wkOrderId: that.data.ordertext,
            admin_id: app.globalData.admin_id,
            // uid:that.data.uc.user_id,
            from: 1,
        }
        if (textarea != '') {
            that.setData({
                textsan: textarea,
            })
        }

        // console.log(formData);
        that.addUserCoursePay1(formData, moneyfree_id)
    },
    addUserCoursePay1(formData, type) {
        var that = this;
        // if (Number(app.globalData.identity) == 3) {
        //     if (that.data.newconid != '' && Number(that.data.newconid) > 0) {
        //         var url = 'crmapi/user/bukuan'
        //     } else {
        //         var url = 'crmapi/Learn/bukuan'
        //     }
        // } else {
        //     var url = 'crmapi/user/bukuan'
        // }
        var url = 'crmapi/Market/bukuan'
        share.wxRequest(url, formData, function(res) {
            var data = res.data;
            wx.hideLoading();
            if (Number(data.errcode) == 0) {
                var data = res.data;
                if (Number(type) == 2) {
                    // var jiaoyanCode = data.data;
                    var jiaoyanCode = CONFIG.API_PIC + data.data.qr_img;
                    that.setData({
                        jiaoyanCode: jiaoyanCode,
                        showerca: true
                    })
                } else {
                    that.setData({
                        showfalse1: true,
                        showfalse2:false
                    })
                }
            } else {
                that.setData({
                    showfalse: true,
                    text: data.msg,
                    showfalse2:false
                })

            }
        })
    },




    // 保存图片到相册
    saveImagephone() {
        share.saveImage('canvas');
    },

    sendImagephone() {
        share.sendImage('canvas');
    },



})